<?php
// +----------------------------------------------------------------------
// | 相册管理
// +----------------------------------------------------------------------
// | Author: 李乾坤 <261001642@qq.com>
// +----------------------------------------------------------------------

class PhotoAction extends CommonAction {
	public function _filter(&$map){
		if($map['title']!=""){
			$map['title']=array('like',"%".$map['title']."%");
		}
	}
	function _Before_index(){
		$Category=D("Category");
		$list=$Category->getCategory('Photo');
		$this->assign('CateList',$list);
	}
    function keupload(){
        $r=array('error'=>0);
        $dir ='Photo/';
        if(empty($_FILES['imgFile']['name'])){
            $r['error']=1;
            $r['message']= '请选择文件!';
        }else{
            import("ORG.Net.UploadFile");
            $upload = new UploadFile();
            $upload->maxSize= 1000000;
            $upload->allowExts  = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
            $upload->savePath =  './Public/Uploads/'.$dir;
            // 记录上传成功ID
            $uploadId =  array();
            $savename = array();
            //执行上传操作
            if(!$upload->upload()) {
                $r['error']=1;
                $r['message']  =  $upload->getErrorMsg();
            }else {
                 //取得成功上传的文件信息
                $f = $upload->getUploadFileInfo();
                $f=$f[0];
                $filename=$f['savepath'].$f['savename'];
                $image =  getimagesize($filename);
                if(false !== $image) {
                    //是图像文件生成缩略图
                    $thumbWidth     =   300;
                    $thumbHeight        =   300;
                    $thumbPrefix        =   'thumb_';
                    // 生成图像缩略图
                    import('ORG.Util.Image');
                    $thumbname  =   $f['savepath'].$thumbPrefix.$f['savename'];
                    Image::thumb($filename,$thumbname,'',$thumbWidth,$thumbHeight,true);
                }
                $data['uid']       =1;
                $data['cid']       =$_GET['cid'];
                $data['img']       =$filename;
                $data['thu_img']   =$thumbname;
                $data['format']    =$f['extension'];
                $data['title']     =empty($_POST['title']) ? $f['name'] : $_POST['title'];
                $Photo=M('Photo');
                if($result = $Photo->data($data)->add()) {
                    $r['message']='SUCCESS';
                }else {
                    $r['message']='操作失败';
                }
                $r['url']=$filename;
            }
        }
        echo json_encode($r);
    }
    public function delete(){
        //删除指定记录
        $model        = M('Photo');
        if(!empty($model)) {
            $id         = $_REQUEST["id"];
            if(isset($id)) {
                $condition = array("id"=>array('in',explode(',',$id)));
                $aaa=$model->where($condition)->select();
                if(false !== $model->where($condition)->delete()){
                    foreach($aaa as $key=>$val){
                        unlink($val['img']);
                        unlink($val['thu_img']);
                    }
                    $this->success(L('删除成功'));
                }else {
                    $this->error(L('删除失败'));
                }
            }else {
                $this->error('非法操作');
            }
        }
    }    
}

?>